<?php
namespace plugin\ky_shouyin\app\controller;

use support\Request;
use support\Response;
use support\Db;

class StaffController
{
    /**
     * 员工列表页面
     */
    public function index()
    {
        return view('staff/index');
    }

    /**
     * 获取员工列表数据
     */
    public function list(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 15);
        $keyword = $request->input('keyword', '');
        
        $query = Db::table('ky_staff')
            ->leftJoin('ky_store', 'ky_staff.store_id', '=', 'ky_store.id')
            ->select('ky_staff.*', 'ky_store.name as store_name');

        if ($keyword) {
            $query->where('ky_staff.name', 'like', "%{$keyword}%");
        }

        $total = $query->count();
        $staff = $query->offset(($page - 1) * $limit)
                      ->limit($limit)
                      ->get();

        return json(['code' => 0, 'msg' => 'success', 'count' => $total, 'data' => $staff]);
    }

    /**
     * 添加员工
     */
    public function add(Request $request)
    {
        if ($request->method() === 'POST') {
            $data = $request->post();
            // 处理添加逻辑
            return json(['code' => 0, 'msg' => '添加成功']);
        }
        return view('staff/add');
    }

    /**
     * 编辑员工
     */
    public function edit(Request $request)
    {
        if ($request->method() === 'POST') {
            $data = $request->post();
            // 处理编辑逻辑
            return json(['code' => 0, 'msg' => '编辑成功']);
        }
        $id = $request->get('id');
        $staff = Db::table('ky_staff')->find($id);
        return view('staff/edit', ['staff' => $staff]);
    }

    /**
     * 删除员工
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        // 处理删除逻辑
        return json(['code' => 0, 'msg' => '删除成功']);
    }

    /**
     * 个人资料
     */
    public function profile(Request $request)
    {
        if ($request->method() === 'POST') {
            $data = $request->post();
            // 处理更新逻辑
            return json(['code' => 0, 'msg' => '更新成功']);
        }
        return view('staff/profile');
    }

    /**
     * 修改密码
     */
    public function password(Request $request)
    {
        if ($request->method() === 'POST') {
            $data = $request->post();
            // 处理密码修改逻辑
            return json(['code' => 0, 'msg' => '密码修改成功']);
        }
        return view('staff/password');
    }
} 